import "./index.scss";
import { Card, Button, message, Form, Input } from "antd";
import { useDispatch } from "react-redux";
import { login } from "../../store/modules/user";
import { useNavigate } from "react-router-dom";

const Login = () => {
  const dispatch = useDispatch();
  const navigate = useNavigate();

  const onFinish = async (values) => {
    await dispatch(login(values));
    message.success("登录成功");
    navigate("/");
  };
  const onFinishFailed = (errorInfo) => {
    console.log("Failed:", errorInfo);
  };

  return (
    <div className="login">
      <Card style={{ width: 380 }}>
        <div className="title">xear</div>
        <Form
          name="basic"
          validateTrigger="onBlur"
          initialValues={{ remember: true }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item
            name="mobile"
            rules={[
              {
                required: true,
                message: "请输入手机号!",
              },
              {
                pattern: /^1[3-9]\d{9}$/,
                message: "请输入正确的手机号!",
              },
            ]}
          >
            <Input placeholder="请输入手机号" allowClear />
          </Form.Item>

          <Form.Item
            name="code"
            rules={[{ required: true, message: "请输入验证码!" }]}
          >
            <Input placeholder="请输入验证码" allowClear />
          </Form.Item>

          <Form.Item>
            <Button type="primary" htmlType="submit">
              登录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  );
};

export default Login;
